﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.BusinessEntity.PPSM2007;
using JM.JCY.Proxy;
using Microsoft.SharePoint;
using JM.JCY.BusinessEntity;
using System.Linq;
using System.Data;
using JM.JCY.Common;

namespace JM.JCY.SharePointComponent.PersonalInformationDocument.YearAssessmentRecord
{
    public partial class YearAssessmentRecordUserControl : UserControl
    {
        PPSM2007CodeDataSet.T_PPSM2007CODEDataTable CodeTable;
        int ID = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CodeTable = GetPPSM2007CODEData();
                //SPWeb web = SPContext.Current.Web;
                var currentUserName = SPContext.Current.Web.CurrentUser.Name;
                //web.CurrentUser.Name;


                DataRow[] assessmentRows = GetAssessmentInfoByName(currentUserName).Select();
                dlYearAssessment.DataSource = assessmentRows;
                dlYearAssessment.DataBind();
                if (assessmentRows.Count() > 0)
                {
                    
                    YearAssessmentNoRecord.Visible = false;
                }
                  
                else
                    YearAssessmentNoRecord.Visible = true;
               

                DataRow[] awardRows = GetAwardInfoByName(currentUserName).Select();
                dlAward.DataSource = awardRows;
                dlAward.DataBind();
                if (awardRows.Count() > 0)
                {
                  
                    AwardInformationNoRecord.Visible = false;
                }
                   
                else
                    AwardInformationNoRecord.Visible = true;
              

                DataRow[] punishRows = GetPunishInfoByName(currentUserName).Select();
                dlPunish.DataSource = punishRows;
                dlPunish.DataBind();
                if (punishRows.Count() > 0)
                {
                   
                    PunishInformationNoRecord.Visible = false;
                }
                   
                else
                    PunishInformationNoRecord.Visible = true;
            }
        }
        public PPSM2007CodeDataSet.T_PPSM2007CODEDataTable GetPPSM2007CODEData()
        {
            var dt = new PPSM2007CodeDataSet.T_PPSM2007CODEDataTable();
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GetPPSM2007CodeTable();
            }
        }
        public PPSMUserAssessmentDataSet.UserAssessmentDataTable GetAssessmentInfoByName(string userName)
        {
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GeAssessmentInfoByName(userName);
            }
        }

        public PPSMUserAssessmentDataSet.UserGloryDataTable GetAwardInfoByName(string userName)
        {
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GeGloryInfoByName(userName);
            }
        }
        public PPSMUserAssessmentDataSet.UserPunishInfoDataTable GetPunishInfoByName(string userName)
        {
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GetPunishInfoByName(userName);
            }
        }


        public string GetCodeDescByCategoryAndValue(PPSM2007CodeDataSet.T_PPSM2007CODEDataTable dt, string category, string value)
        {
            try
            {
                var codeDesc = from codeTable in dt
                               where codeTable.Category == category && codeTable.CodeValue == value
                               select codeTable.CodeDesc;
                return codeDesc.First();
            }
            catch (Exception)
            {

                return "";
            }
          


        }
        protected void dlYearAssessment_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            PPSMUserAssessmentDataSet.UserAssessmentRow row = (PPSMUserAssessmentDataSet.UserAssessmentRow)e.Item.DataItem;
            Label lbYearAssessment = (Label)e.Item.FindControl("lbYearAssessment");
            Label lbAssessmentResult = (Label)e.Item.FindControl("lbAssessmentResult");
            lbYearAssessment.Text = string.IsNullOrEmpty(row.UserTestTime) ? string.Empty : row.UserTestTime;
            lbAssessmentResult.Text = string.IsNullOrEmpty(row.UserTestLevel) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "考核情况",row.UserTestLevel);
        }

        protected void dlAward_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            PPSMUserAssessmentDataSet.UserGloryRow row = (PPSMUserAssessmentDataSet.UserGloryRow)e.Item.DataItem;
            Label lbAwardDate = (Label)e.Item.FindControl("lbAwardDate");
            Label lbAwardName = (Label)e.Item.FindControl("lbAwardName");
            Label lbAwardPlaceName = (Label)e.Item.FindControl("lbAwardPlaceName");
            lbAwardDate.Text = string.IsNullOrEmpty(row.GloryDate) ? string.Empty : row.GloryDate.Substring(0, 4).ToString() + "年" + row.GloryDate.Substring(4, 2).ToString() + "月" + row.GloryDate.Substring(6, 2).ToString() + "日";
            lbAwardName.Text = string.IsNullOrEmpty(row.GloryArea) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "奖励范围", row.GloryArea);
            lbAwardPlaceName.Text = string.IsNullOrEmpty(row.GloryByUnitName) ? string.Empty : row.GloryByUnitName;
        }

        protected void dlPunish_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            PPSMUserAssessmentDataSet.UserPunishInfoRow row = (PPSMUserAssessmentDataSet.UserPunishInfoRow)e.Item.DataItem;
            Label lbPunishDate = (Label)e.Item.FindControl("lbPunishDate");
            Label lbPunishReason = (Label)e.Item.FindControl("lbPunishReason");
            Label lbPunishNumber = (Label)e.Item.FindControl("lbPunishNumber");
            Label lbPunishPlace = (Label)e.Item.FindControl("lbPunishPlace");
            lbPunishDate.Text = string.IsNullOrEmpty(row.PunishDate) ? string.Empty : row.PunishDate.Substring(0, 4).ToString() + "年" + row.PunishDate.Substring(4, 2).ToString() + "月" + row.PunishDate.Substring(6, 2).ToString() + "日";
            lbPunishReason.Text = string.IsNullOrEmpty(row.PunishReason) ? string.Empty : row.PunishReason;
            lbPunishNumber.Text = string.IsNullOrEmpty(row.PunishFileNumber) ? string.Empty : row.PunishFileNumber;
            lbPunishPlace.Text = string.IsNullOrEmpty(row.PunishByUnitName) ? string.Empty : row.PunishByUnitName;
        }
    }
}
